﻿Public Class CarHDStartUpService

    Public Shared Function GetHDStartUpCarList(ByVal projectId As String, ByVal startTime As DateTime, ByVal endTime As DateTime) As IList(Of ProjectInfo.CarHDStartUpItem)

        Dim carList As IList(Of ProjectInfo.CarHDStartUpItem)
        carList = New List(Of ProjectInfo.CarHDStartUpItem)

        Dim dt As DataSet
        Dim strsql As String = ""
        strsql += String.Format("select * from tbcchiststartup_{0}_1 where BeginTime between '{1}' and '{2}'", projectId, startTime, endTime)

        dt = TBCC.Utility.SqlHelper.ExecuteDataset(strsql, Nothing)
        For Each r As DataRow In dt.Tables(0).Rows
            Dim car As New ProjectInfo.CarHDStartUpItem
            car.Id = SqlHelper.GetDbData(r("Id"))
            car.BeginTime = SqlHelper.GetDbData(r("BeginTime"))
            car.EndTime = SqlHelper.GetDbData(r("EndTime"))
            ' car.PageIndex = SqlHelper.GetDbData(r("PageIndex"))
            'car.PageCount = SqlHelper.GetDbData(r("PageCount"))
            car.RecordInterval = SqlHelper.GetDbData(r("RecordInterval"))
            'car.LastPageIndex = SqlHelper.GetDbData(r("LastPageIndex"))
            'car.LastPageType = SqlHelper.GetDbData(r("LastPageType"))
            car.BeginAddress = SqlHelper.GetDbData(r("BeginAddress"))
            car.EndAddress = SqlHelper.GetDbData(r("EndAddress"))
            car.Shipper = SqlHelper.GetDbData(r("Shipper"))
            car.Carrier = SqlHelper.GetDbData(r("Carrier"))
            car.Receiver = SqlHelper.GetDbData(r("Receiver"))
            'car.LastRecordTime = SqlHelper.GetDbData(r("LastRecordTime"))
            car.UpdateStatus = SqlHelper.GetDbData(r("UpdateStatus"))

            carList.Add(car)

        Next
        Return carList
    End Function
    Public Shared Function GetHDStartUpCarList(ByVal projectId As String) As IList(Of ProjectInfo.CarHDStartUpItem)
        Dim carList As IList(Of ProjectInfo.CarHDStartUpItem)
        carList = New List(Of ProjectInfo.CarHDStartUpItem)

        Dim dt As DataSet
        Dim strsql As String = ""
        strsql += String.Format("select * from tbcchiststartup_{0}_1 where finished=0 order by BeginTime", projectId)

        dt = TBCC.Utility.SqlHelper.ExecuteDataset(strsql, Nothing)
        For Each r As DataRow In dt.Tables(0).Rows
            Dim car As New ProjectInfo.CarHDStartUpItem
            car.Id = SqlHelper.GetDbData(r("Id"))
            car.BeginTime = SqlHelper.GetDbData(r("BeginTime"))
            car.EndTime = SqlHelper.GetDbData(r("EndTime"))
            car.RecordInterval = SqlHelper.GetDbData(r("RecordInterval"))
            car.BeginAddress = SqlHelper.GetDbData(r("BeginAddress"))
            car.EndAddress = SqlHelper.GetDbData(r("EndAddress"))
            car.Shipper = SqlHelper.GetDbData(r("Shipper"))
            car.Carrier = SqlHelper.GetDbData(r("Carrier"))
            car.Receiver = SqlHelper.GetDbData(r("Receiver"))
            car.UpdateStatus = SqlHelper.GetDbData(r("UpdateStatus"))

            carList.Add(car)

        Next
        Return carList

    End Function
    Public Shared Function GetMaxHDStartUpCar(ByVal projectId As String) As Integer

        Dim strSql As String = ""
        strSql += String.Format("select max(id) from tbcchiststartup_{0}_1  order by BeginTime", projectId)
        Dim obj As Object
        obj = SqlHelper.ExecuteScalar(strSql, Nothing)

        If obj Is Nothing OrElse IsDBNull(obj) Then
            Return -1
        End If

        Return CInt(obj)

    End Function

    Public Shared Function SetStartStopDB(ByVal pi As String, ByVal carstartstop() As CarWebServiceAdd.StartUpView) As Boolean
        Dim strSql As String
        Dim tic As New TBCC.Utility.TranstrationItem

        strSql = String.Format("Insert Into tbcchiststartup_{0}_1(id,BeginTime,EndTime,RecordInterval,BeginAddress,EndAddress,Shipper,Carrier,Receiver,UpdateStatus,Finished) Values(?p0,?p1,?p2,?p3,?p4,?p5,?p6,?p7,?p8,?p9,?p10);-- ", pi)
        For k As Integer = 0 To carstartstop.Length - 1
            Dim CarStartStopClass As New CarWebServiceAdd.StartUpView
            CarStartStopClass = carstartstop(k)
            Dim p(10) As MySql.Data.MySqlClient.MySqlParameter
            For i As Integer = 0 To p.GetUpperBound(0)
                p(i) = New MySql.Data.MySqlClient.MySqlParameter
                p(i).ParameterName = "p" + i.ToString
            Next
            p(0).Value = CarStartStopClass.id
            p(1).Value = CarStartStopClass.beginTime
            p(2).Value = CarStartStopClass.endTime
            p(3).Value = CarStartStopClass.recordInterval
            p(4).Value = CarStartStopClass.beginAddress
            p(5).Value = CarStartStopClass.endAddress
            p(6).Value = CarStartStopClass.shipper
            p(7).Value = CarStartStopClass.carrier
            p(8).Value = CarStartStopClass.receiver
            p(9).Value = CarStartStopClass.updateStatus
            p(10).Value = 0


            tic.Add(strSql & k.ToString, p)
        Next
        

        Return SqlHelper.ExecuteTranstration(tic)
    End Function
    Public Shared Function GetStartStopSpace(ByVal pi As String, ByVal id As Integer) As Integer

        Dim strSql As String = ""
        strSql += String.Format("select RecordInterval from tbcchiststartup_{0}_1 where id={1} order by BeginTime", pi, id)
        Dim obj As Object
        obj = SqlHelper.ExecuteScalar(strSql, Nothing)

        If obj Is Nothing OrElse IsDBNull(obj) Then
            Return -1
        End If

        Return CInt(obj)

    End Function
    Public Shared Function UpdateStartStopDB(ByVal pi As String, ByVal StartStopid As Integer, ByVal carHD() As CarWebServiceAdd.CarHisView) As Boolean
        Dim strSql As String
        Dim tic As New TBCC.Utility.TranstrationItem
        strSql = String.Format("update  tbcchiststartup_{0}_1 set endtime=?p0,Finished=?p1 where id=?p2", pi)

        Dim p(2) As MySql.Data.MySqlClient.MySqlParameter
        For i As Integer = 0 To p.GetUpperBound(0)
            p(i) = New MySql.Data.MySqlClient.MySqlParameter
            p(i).ParameterName = "p" + i.ToString
        Next
        p(0).Value = carHD(0).endTime
        p(1).Value = 1
        p(2).Value = StartStopid


        tic.Add(strSql, p)

        Return SqlHelper.ExecuteTranstration(tic)
    End Function
End Class
